/*
	Copyright (c) 2009-2011, Stefan Webb.

	This file is part of SimpleML. Please read the file "license.txt", 
	which contains the conditions under which this software is distributed.
*/

/*!
	\file pointerstack.h
	\author Stefan Webb
	\brief Definition of the PointerStack class template.
*/

#ifndef POINTERSTACK_H
#define POINTERSTACK_H

//#include "stdafx.h"

/*! \brief Class for pointer stack. This class differs from stack in that the elements are assumed to be 
	pointers to memory declared by new. Therefore, all elements will be deleted before destructing. */
template <class T = utf16> class PointerStack : public Stack<T>
{
public:
	/*! \brief Default constructor. */
	PointerStack();

	/*! \brief Constructor with capacity.
		\param capacity Initial and minimum capacity of pointer stack. */
	PointerStack(unsigned int capacity);

	/*! \brief Default destructor. */
	~PointerStack();

	/*! \brief Create new bucket with identical initial and minimum capacity.
		\param capacity Initial and minimum capacity of pointer stack. */
	void create(unsigned int capacity);

	/*! \brief Create new bucket with given initial and minimum capacity. 
		\param initialCapacity Initial capacity of pointer stack.
		\param minimumCapacity Minimum capacity of pointer stack. */
	void create(unsigned int initialCapacity, unsigned int minimumCapacity);

	/*! \brief Reset stack. */
	void reset();

	/*! \brief Free memory for stack members. */
	void destroy();

	/*! \brief Chuck out contents of bucket. */
	void empty();

};

/* Default constructor. */
template <class T> PointerStack<T>::PointerStack() : Stack() {}

/* Constructor with initial size. */
template <class T> PointerStack<T>::PointerStack(unsigned int capacity) : Stack(capacity) {}

/* Default destructor. */
template <class T> PointerStack<T>::~PointerStack()
{
	destroy();
}



/* Reset stack. */
template <class T> void PointerStack<T>::reset()
{
	// Free memory for stack members
	empty();

	// Reset
	Bucket<T>::reset();
}

/* Free memory for stack members. */
template <class T> void PointerStack<T>::destroy()
{
	// Empty bucket
	empty();

	// Delete bucket
	Bucket<T>::destroy();
}

/* Chuck out contents of bucket. */
template <class T> void PointerStack<T>::empty()
{
	// Delete stack members
	for (unsigned int i = 0; i < size_; i++)
		delete [] array_[i];
}

/* Create new bucket with identical initial and minimum capacity. */
template <class T> void PointerStack<T>::create(unsigned int capacity)
{
	create(capacity, capacity);
}

/* Create new bucket with given initial and minimum capacity. */
template <class T> void PointerStack<T>::create(unsigned int initialCapacity, unsigned int minimumCapacity)
{
	// Delete pointer if needed
	empty();

	// Create bucket
	Bucket<T>::create(initialCapacity, minimumCapacity);
}



#endif